<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use app\admin\model\AdminModel;

class YyOrderCommon extends Model
{
    protected $map = array();
    public function __construct()
    {
        parent::__construct();
        $areaInfo = AdminModel::getRoleArea();
        if(!empty($areaInfo)){
            $this->map['city_id'] = ['in',implode(',',$areaInfo['city'])];
        }
    }

    //订单列表
    public function getListInfo($page, $limit, $data)
    {
        $where  = self::searchWhere($data);
        //分页
        $start  = ($page - 1) * $limit;
        $count  = Db::table('yy_order_common')->where($where)->count();
        $list   = Db::table('yy_order_common')->where($where)->limit($start, $limit)->order('common_id desc')->select();
        foreach ($list as $key=>$value){
            //查询 省份 - 城市
            if(!empty($value['province_id'])){
                $province_name = Db::name('region')->where(['region_id'=>$value['province_id']])->value('region_name');
            }else{
                $province_name = '-';
            }
            if(!empty($value['city_id'])){
                $city_name = Db::name('region')->where(['region_id'=>$value['city_id']])->value('region_name');
            }else{
                $city_name = '-';
            }
            $list[$key]['adress_name']  = $province_name.' / '.$city_name;
            $list[$key]['order_time']   = !empty($value['order_time'])?date("Y-m-d H:i:s",$value['order_time']):'-';
            $list[$key]['dep_time']     = !empty($value['dep_time'])?date("Y-m-d H:i:s",$value['dep_time']):'-';
            $list[$key]['dest_time']    = !empty($value['dest_time'])?date("Y-m-d H:i:s",$value['dest_time']):'-';
            $list[$key]['orderStatus']  = order_status($value['order_state']);
            $list[$key]['price']        = sprintf("%.2f", $value['price'] / 100);
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }

    public function getOrderInfo($data){
        $where = self::searchWhere($data);
        $list   = Db::table('yy_order_common')->where($where)->order('common_id desc')->select();
        foreach ($list as $key=>$value){
            //查询 省份 - 城市
            if(!empty($value['province_id'])){
                $province_name = Db::name('region')->where(['region_id'=>$value['province_id']])->value('region_name');
            }else{
                $province_name = '-';
            }
            if(!empty($value['city_id'])){
                $city_name = Db::name('region')->where(['region_id'=>$value['city_id']])->value('region_name');
            }else{
                $city_name = '-';
            }
            $list[$key]['adress_name']      = $province_name.' / '.$city_name;
            $list[$key]['order_time']       = !empty($value['order_time'])?date("Y-m-d H:i:s",$value['order_time']):'-';
            $list[$key]['depart_time']      = !empty($value['depart_time'])?date("Y-m-d H:i:s",$value['depart_time']):'-';
            $list[$key]['distribute_time']  = !empty($value['distribute_time'])?date("Y-m-d H:i:s",$value['distribute_time']):'-';
            $list[$key]['dep_time']         = !empty($value['dep_time'])?date("Y-m-d H:i:s",$value['dep_time']):'-';
            $list[$key]['dest_time']        = !empty($value['dest_time'])?date("Y-m-d H:i:s",$value['dest_time']):'-';
            $list[$key]['pay_time']         = !empty($value['pay_time'])?date("Y-m-d H:i:s",$value['pay_time']):'-';
            $list[$key]['order_match_time'] = !empty($value['order_match_time'])?date("Y-m-d H:i:s",$value['order_match_time']):'-';
            $list[$key]['cancel_time']      = !empty($value['cancel_time'])?date("Y-m-d H:i:s",$value['cancel_time']):'-';
            $list[$key]['orderStatus']      = order_status($value['order_state']);
            $list[$key]['price']            = sprintf("%.2f", $value['price'] / 100);
            $list[$key]['fact_price']       = sprintf("%.2f", $value['fact_price'] / 100);
            $list[$key]['far_up_price']     = sprintf("%.2f", $value['far_up_price'] / 100);
            $list[$key]['other_up_price']   = sprintf("%.2f", $value['other_up_price'] / 100);
            if($value['pay_state'] == 1){
                $list[$key]['payStateName'] = "已付款";
            }elseif($value['pay_state'] == 2){
                $list[$key]['payStateName'] = "未付款";
            }else{
                $list[$key]['payStateName'] = "-";
            }
        }
        return $list;
    }

    //检索条件处理
    public function searchWhere($data = [])
    {
        $where = $this->map;
        //订单ID检索
        if (!empty($data['common_id'])) {
            $where['common_id'] = $data['common_id'];
        }
        //城市搜索
        if (!empty($data['province_id']) && !empty($data['city_id'])) {
            $where['province_id']   = $data['province_id'];
            $where['city_id']       = $data['city_id'];
        } else {
            //省份搜索
            if (!empty($data['province_id'])) {
                //查询该省份所以城市ID
                $cityList = Db::name('region')->where('parent_id', $data['province_id'])->field('region_id')->select();
                if (!empty($cityList)) {
                    $city_id = '';
                    foreach ($cityList as $value) {
                        $city_id .= $value['region_id'] . ',';
                    }
                    $city_id = rtrim($city_id, ',');
                    $areaList = Db::name('region')->where(['parent_id' => ['in', rtrim($city_id, ',')]])->field('region_id')->select();
                    foreach ($areaList as $value) {
                        $city_id .= $value['region_id'] . ',';
                    }
                    $where['city_id'] = ['in', rtrim($city_id, ',')];
                }
            }
        }
        //下单时间
        if (!empty($data['start_time'])) {
            $where['order_time'] = ['>=', strtotime($data['start_time'])];
        }
        if (!empty($data['end_time'])) {
            $where['order_time'] = ['<=', strtotime($data['end_time'] . " 23:59:59")];
        }
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where['order_time'] = ['between', [strtotime($data['start_time']), strtotime($data['end_time'] . " 23:59:59")]];
        }
        //订单号
        if (!empty($data['order_sn'])) {
            $where['order_id'] = ['like', "%" . $data['order_id'] . "%"];
        }
        //司机手机号
        if (!empty($data['driver_phone'])) {
            $where['driver_phone'] = ['like', "%" . $data['driver_phone'] . "%"];
        }
        //司机姓名
        if (!empty($data['driver_name'])) {
            $where['driver_name'] = ['like', "%" . $data['driver_name'] . "%"];
        }
        //订单状态
        if ($data['order_status'] != '') {
            if ($data['order_status'] == 45) {
                $where['order_state'] = ['>=', $data['order_status']];
            } else {
                $where['order_state'] = $data['order_status'];
            }
        }
        return $where;
    }
}
